
// javascript for widget environment

// update system status information
function updateStatus() {
    var sysinfo = document.embeds[0];
    var battery = sysinfo.chargelevel;
    var signal = sysinfo.signalbars;
    var memtotal = sysinfo.totalram;
    var memfree = sysinfo.freeram;

    $('stat_bat').firstChild.nodeValue = battery+"%";
    $('stat_sig').firstChild.nodeValue = signal+"/7";
    $('stat_memtot').firstChild.nodeValue = memtotal/1024+"KB";
    $('stat_memfree').firstChild.nodeValue = memfree/1024+"KB";
}
function onRightKey() {
    alert("You pressed the right soft key");
}

// init widget tab
function initWidget() {
    if (!window.menu) {
        // not in a widget environment

        // hide widget info
        $('tabwidget').getElementsByTagName('div')[0].style.display = 'none';
        return;
    }

     $('tabwidget').getElementsByTagName('p')[1].style.display = 'none';

    // running as widget, install menus
    createMenus();

    updateStatus();

    // set right softkey
    window.menu.setRightSoftkeyLabel('Hello', onRightKey);
}



// change the screen orientation
function onLandscape() {
    if (window.widget.isrotationsupported)
        window.widget.setDisplayLandscape();
}
// change the screen orientation
function onPortrait() {
    if (window.widget.isrotationsupported)
        window.widget.setDisplayPortrait();
}
// make phone beep
function onBeep() {
    var sysinfo = document.embeds[0];
    var frequency = 2000;
    var duration = 2000000;
    sysinfo.beep(frequency, duration);
}
// make phone vibrate
function onVibrate() {
    var sysinfo = document.embeds[0];
    var intensity = 100;
    var duration = 2000;
    sysinfo.startvibra(duration, intensity);
}

// menu item ids
CMD_LAND   = 100;
CMD_PORT   = 101;
CMD_BEEP   = 102;
CMD_VIB    = 103;
// menu items specified in an array of
// (display text, cmd id, callback function, item handle)
var menu_items = [["Landscape", CMD_LAND, onLandscape, 0],
    ["Portrait", CMD_PORT, onPortrait, 0],
    ["Beep", CMD_BEEP, onBeep, 0],
    ["Vibrate", CMD_VIB, onVibrate, 0],
    ];

// create menus from the above array
function createMenus() {
    for (i = 0; i < menu_items.length; i++) {
        var item = menu_items[i];
        var handle = new MenuItem(item[0], item[1]);
        item.onSelect = onMenuItemSelected;
        item[3] = handle;
        window.menu.append(handle);
     }
}
// process menu selections
function onMenuItemSelected(cmd_id) {
    for (i = 0; i < menu_items.length; i++) {
        var item = menu_items[i];
        if (item[1] == cmd_id) {
            item[2]();
            break;
        }
    }
}

